package com.hs.pre.sys.user;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

import com.hs.HsBase;
import com.hs.core.db.DBOperator;
import com.hs.core.db.DatabaseConnection;
import com.hs.core.db.HsPreparedStatement;
import com.hs.core.db.HsResultSet;
import com.hs.pre.model.SortBean;
import com.hs.pre.model.UserBean;
import com.hs.pre.page.PageParam;
import com.hs.pre.page.PageQuery;
import com.hs.pre.page.PageUtil;
import com.hs.pre.page.ReverseLoadData;
import com.hs.pre.util.MD5;

public class UserDAO {

	
	public PageUtil getUserInfoList(PageParam pageParam,String userName)
	{
		String sql = (String) HsBase.getAnsiSqlMap().get("pre_user.select");
		List par = new ArrayList();
		if(userName != null && !(userName=userName.trim()).equals(""))
		{
			sql += " and userName like ? ";
			par.add("%"+userName+"%");
			pageParam.getParamMap().put("userName", userName);
		}
		Object[] oarTem = null;
		if(!par.isEmpty())
			oarTem = par.toArray();

		try {
			PageQuery a= new PageQuery(sql,oarTem,pageParam,new ReverseLoadData(){
				public Object load(Map map){
					UserBean userBean = new UserBean();
					userBean.setUserId((Long)map.get("userID"));
					userBean.setLoginId((String)map.get("loginId"));
					userBean.setUserName((String)map.get("userName"));
					if(map.get("isAdmin") != null)
						userBean.setIsAdmin(((Integer)map.get("isAdmin")).intValue());
					if(map.get("isDelete") != null)
						userBean.setIsDelete(((Integer)map.get("isDelete")).intValue());
					userBean.setEmail((String)map.get("email"));
					userBean.setCreateIp((String)map.get("createIp"));
					
					return userBean;
				}
			});
			return a.getPageUtil();
		} catch (Exception e) { 
			e.printStackTrace();
		}
		return null;
		
	}
	
	/**
	 * 添加用户
	 * @param userBean
	 * @return
	 * @throws Exception
	 */
	public UserBean addUser(UserBean userBean) throws Exception
	{	
		String sql = (String) HsBase.getAnsiSqlMap().get("pre_user.insert");
		HsPreparedStatement st = new HsPreparedStatement(sql,true); 
//		(loginId,pwd,userName,isAdmin,email)
		st.setString(1, userBean.getLoginId());
		st.setString(2, MD5.md5(userBean.getPwd()));
		st.setString(3, userBean.getUserName());
		st.setInt(4, userBean.getIsAdmin());
		st.setString(5, userBean.getEmail());
		st.setString(6, userBean.getCreateIp());
		String key = st.executeUpdateReturnId();
		userBean.setUserId(Long.parseLong(key));
		return userBean;
	}
	
	/**
	 *  删除用户
	 * @param userId
	 * @return
	 */
	public boolean delUser(List va)
	{
		String sql = (String) HsBase.getAnsiSqlMap().get("pre_user.delete");
		HsPreparedStatement st = new HsPreparedStatement(sql);
		try {
			st.executeUpdate(va);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return false;
	}
	
	public UserBean findBean(String userId) throws Exception
	{
		if(userId == null)
			return null;
		String sql = (String) HsBase.getAnsiSqlMap().get("pre_user.findBean");
		DBOperator dbo = null;
		try {
			dbo = DatabaseConnection.getDBOperator();
		} catch (Exception e) { 
		}
		if(dbo == null)
			return null;
		List list02 = dbo.select(sql, new Object[]{userId});
		if(!list02.isEmpty())
		{
			Map map = (Map)list02.get(0);
			UserBean userBean = new UserBean();
			userBean.setUserId((Long)map.get("userId"));
			userBean.setLoginId((String)map.get("loginId"));
			userBean.setUserName((String)map.get("userName"));
			userBean.setIsAdmin((Integer)map.get("isAdmin"));
			userBean.setIsDelete((Integer)map.get("isDelete") );
			userBean.setEmail((String)map.get("email"));
			return userBean;
		}
		return null;
	}
	
	/**
	 * 修改用户
	 * @param userBean
	 * @return
	 * @throws Exception
	 */
	public UserBean editUser(UserBean userBean) throws Exception
	{	
		String sql = (String) HsBase.getAnsiSqlMap().get("pre_user.update");
		HsPreparedStatement st = new HsPreparedStatement(sql); 
//		(loginId,pwd,userName,isAdmin,email)
		st.setString(1, userBean.getLoginId());
		st.setString(2, MD5.md5(userBean.getPwd()));
		st.setString(3, userBean.getUserName());
		st.setInt(4, userBean.getIsAdmin());
		st.setString(5, userBean.getEmail());
		st.setString(6, userBean.getCreateIp());
		st.setInt(7, userBean.getUserId());
		st.executeUpdate();
		return userBean;
	}
}
